package com.interview.controller;

import com.interview.common.Result;
import com.interview.common.ResultMessage;
import com.interview.common.StatusCode;
import com.interview.entity.GroupInfo;
import com.interview.entity.Schedule;
import com.interview.entity.SignedStatics;
import com.interview.service.GroupInfoService;
import com.interview.service.ScheduleService;
import com.interview.service.SignedInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("schedule")
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private GroupInfoService groupInfoService;
    @Autowired
    private SignedInfoService signedInfoService;


    @PostMapping("getScheduleList")
    public Result getScheduleList() {
        List<Schedule> scheduleList=scheduleService.getScheduleList();
        if(scheduleList.size()>0) {
            return new Result(true, StatusCode.OK, ResultMessage.OPERATION_SUCCESS,null);
        }else {
            return new Result(false,StatusCode.ERROR,ResultMessage.OPERATION_FAILED,null);
        }
    }

    @PostMapping("generateExameeDrawLotsPool")
    public Result generateDrawLotsPool() {
        List<GroupInfo> allGroupInfo = groupInfoService.getAllGroupInfo();
        List<Schedule> allSchedules = new LinkedList<>();
        for (GroupInfo groupInfo : allGroupInfo) {
            List<Schedule> groupDrawLotsSchedules = groupInfoService.generateDrawLotsProcess(groupInfo);
            for (Schedule groupDrawLotsSchedule : groupDrawLotsSchedules) {
                allSchedules.add(groupDrawLotsSchedule);
            }
        }
        scheduleService.saveSchedule(allSchedules);
        List<Schedule> scheduleList = scheduleService.getScheduleList();
        if(scheduleList.size()>0) {
            return new Result(true, StatusCode.OK, ResultMessage.OPERATION_SUCCESS,null);
        }else {
            return new Result(false,StatusCode.ERROR,ResultMessage.OPERATION_FAILED,null);
        }
    }

    @PostMapping("clearExameeDrawLotsPool")
    public Result clearDrawLotsPool() {
        int number=scheduleService.clearExameeDrawLotsPool();
        if (number>0) {
            return new Result(true, StatusCode.OK, ResultMessage.OPERATION_SUCCESS,null);
        }else {
            return new Result(false,StatusCode.ERROR,ResultMessage.OPERATION_FAILED,null);
        }
    }

    @PostMapping("getRemainingDrawLots")
    public Result getRemainingDrawLots() {

        //获取缺考人数
        List<SignedStatics> signedStaticsList = signedInfoService.getSignedStaticsList();
        ArrayList<String> strings = new ArrayList<>();
        for (SignedStatics signedStatics : signedStaticsList) {
            if (Integer.parseInt(signedStatics.getNotSigned())!=0){
                String groupId = signedStatics.getGroupId();
                String notSigned = signedStatics.getNotSigned();
                List<String> drawLotsAwaitList = scheduleService.getDrawLotsAwaitList(groupId);
                if (Integer.parseInt(notSigned)==drawLotsAwaitList.size()) {
                    strings.add(groupId);
                }
            }
        }
        if (strings.size()>0) {
            LinkedList<HashMap> list = new LinkedList<>();
            for (String groupId : strings) {
                List<String> drawLotsAwaitList = scheduleService.getDrawLotsAwaitList(groupId);
                String joined = String.join(",", drawLotsAwaitList);
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("groupId",groupId);
                hashMap.put("absent",joined);
                list.add(hashMap);
            }
            return new Result(true, StatusCode.OK, ResultMessage.OPERATION_SUCCESS,list);
        }else {
            return new Result(false,StatusCode.ERROR,ResultMessage.OPERATION_FAILED,null);
        }
    }
}
